home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / nwtp06 / r3_hello.pas < prev    next >
Pascal/Delphi Source File  |  1996-07-10  |  4KB  |  146 lines

  1. {$X+,V-,B-}
  2. program RecHello3;
  3.  
  4. { Simple IPX demonstration program, that uses one receive ESR.
  5.  
  6.   Run this program on 1 workstation, run S_HELLO or S1_HELLO on another.
  7.   S_HELLO will send "hello world" messages,
  8.   this workstation will receive them.  }
  9.  
  10. { This program consists of two concurrent processes:
  11.  
  12.   1. (background) An ESR that fills a global receive buffer with
  13.                   incoming packets. Only when the buffer is full
  14.                   will packets be discarded.
  15.  
  16.   2. (foreground) A process that performs its regular task. Whenever
  17.                   there is time, the process will process the
  18.                   received packets. }
  19.  
  20. uses crt,nwMisc,nwIPX;
  21.  
  22. CONST IOSocket=$5678;
  23.  
  24. Var ReceiveEcb    :Tecb;
  25.     IpxHdr        :TipxHeader;
  26.     socket        :word;
  27.     buf           :array[1..546] of byte;
  28.     t             :byte;
  29.     ReceivedBufLen:word;
  30.  
  31.     ReceivedMsg:array[1..100] of record
  32.                                  InUse:Boolean;
  33.                                  Message:string[25];
  34.                                  end;
  35.  
  36.     EsrBufInd  :Byte; { used by ESR }
  37.  
  38.     NewStack:array[1..1024] of word;  { !! used by ESR }
  39.     StackBottom:word;                 { !! used by ESR }
  40.  
  41.  
  42. {$F+}
  43. Procedure ListenESRhandler(Var p:Tpecb);
  44. begin
  45. { look for an empty spot in the global buffer }
  46. EsrBufInd:=1;
  47. while (EsrBufInd<=100) and ReceivedMsg[EsrBufInd].Inuse
  48.  do inc(EsrBufInd);
  49. IF EsrBufInd<=100
  50.  then begin
  51.       { empty place found. Insert msg }
  52.       with ReceivedMsg[EsrBufInd]
  53.        do begin
  54.           Message[0]:=chr(p^.fragment[2].size);
  55.           if Message[0]>#25 then Message[0]:=#25;
  56.           move(p^.fragment[2].address^,Message[1],ord(Message[0]));
  57.           InUse:=True;
  58.           end;
  59.       end
  60.  else ; { entire buffer is filled => discard packet }
  61. { Setup to listen for next incoming packet }
  62. IPXListenForPacket(ReceiveECB);
  63. end;
  64. {$F-}
  65.  
  66. {$F+}
  67. Procedure ListenESR; assembler;
  68. asm { ES:SI are the only valid registers when entering this procedure ! }
  69.     mov dx, seg stackbottom
  70.     mov ds, dx
  71.  
  72.     mov dx,ss  { setup of a new local stack }
  73.     mov bx,sp  { ss:sp copied to dx:bx}
  74.     mov ax,ds
  75.     mov ss,ax
  76.     mov sp,offset stackbottom
  77.     push dx    { push old ss:sp on new stack }
  78.     push bx
  79.  
  80.     push es    { push es:si on stack as local vars }
  81.     push si
  82.     mov  di,sp
  83.  
  84.     push ss    { push address of local ptr on stack }
  85.     push di
  86.     CALL ListenEsrHandler
  87.  
  88.     add sp,4   { skip stack ptr-copy }
  89.     pop bx     { restore ss:sp from new stack }
  90.     pop dx
  91.     mov sp,bx
  92.     mov ss,dx
  93. end;
  94. {$F-}
  95.  
  96.  
  97. begin
  98. IF NOT IpxInitialize
  99.  then begin
  100.       writeln('Ipx needs to be installed.');
  101.       halt(1);
  102.       end;
  103. socket:=IOSocket;
  104. IF NOT IPXopenSocket(Socket,SHORT_LIVED_SOCKET)
  105.  then begin
  106.       writeln('IPXopenSocket returned error# ',nwIPX.result);
  107.       halt(1);
  108.       end;
  109.  
  110. FillChar(buf,546,#0);
  111.  
  112. { Setup ECB and IPX header }
  113. IPXsetupListenECB(Addr(ListenESR),IOsocket,@buf,546,
  114.                   IpxHdr,ReceiveEcb);
  115.  
  116. IPXListenForPacket(ReceiveECB);
  117.  
  118. writeln('ESR will start filling a global buffer with packets received.');
  119. writeln('Starting foreground process...');
  120. writeln;
  121. writeln('Foreground process just writes a ''dot'' to the screen every second.');
  122. writeln('When a key is pressed, this process is terminated and the received');
  123. writeln('packets are shown.');
  124.  
  125. REPEAT
  126. IPXrelinquishControl;
  127. delay(1000);
  128. write('.');
  129. UNTIL KeyPressed;
  130.  
  131. writeln;
  132. writeln('Dumping global receive buffer -- filled by background process.');
  133.  
  134. for t:=1 to 100
  135.  do if ReceivedMsg[t].Inuse
  136.   then begin
  137.        writeln(ReceivedMsg[t].Message);
  138.        ReceivedMsg[t].Inuse:=False; { give entry in buffer free }
  139.        end;
  140. { You may also choose to process just 1 entry in the received buffer.
  141.   Set Inuse to False after processing, so the ESR can fill it again }
  142.  
  143. IF NOT IPXcloseSocket(IOsocket)
  144.  then writeln('IPXcloseSocket returned error# ',nwIPX.result);
  145.  
  146. end.